package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.AreaDao;
import com.gxuwz.medical.domain.area.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 行政区域管理控制模块
 * @author 沉羽墨玖
 *
 */
public class AreaServlet extends BaseServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");
		if ("list".equals(m)) {
			process(req, resp, "/page/area/area_list.jsp");
		} else if ("input".equals(m)) {
			process(req, resp, "/page/area/area_add.jsp");
		} else if ("get".equals(m)) {
			process(req, resp, "/page/area/area_edit.jsp");
		} else if ("add".equals(m)) {
			add(req, resp);
		} else if ("edit".equals(m)) {
			edit(req, resp);
		} else if ("del".equals(m)) {
			del(req, resp);
		} else if ("groupList".equals(m)) {
			groupList(req, resp);

		} else {
			error(req, resp);
		}
	}

	private void groupList(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String villageID = req.getParameter("villageID");
		AreaDao areaDao = new AreaDao();
		try {
			List<Area> areaList = areaDao.findChildArea(villageID, 4); //根据行政区域获取所有组并存放到list中
			String respBody = JSON.toJSONString(areaList); //转换为JSON格式
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter(); //向前台的jsp页面输出结果
			System.out.println("respBody:" + respBody);
			out.print(respBody);
			out.flush(); //把输出流里面的数据输出
			out.close(); //关闭输出流
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String areapid = req.getParameter("areacode");
		String areaName = req.getParameter("areaName");
		Area area = new Area();
		try {

			area.addArea(areapid, areaName);
			process(req, resp, "/page/area/area_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void edit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String areacode = req.getParameter("areacode");
		String areaName = req.getParameter("areaName");
		int grade = areacode.length() == 6 ? 1 : 2; //根据行政区域的长度判断级别，6位为县，8位为镇，10位为村，12位为组
		Area area = new Area(areacode, areaName, grade);
		try {
			area.edit();
			process(req, resp, "/page/area/area_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void del(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String areacode = req.getParameter("areacode");
		Area area = new Area();
		try {
			System.out.println(areacode);
			area.del(areacode);
			process(req, resp, "/page/area/area_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
